package com.example.worldcupsever;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.worldcupsever.bean.User;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "LoginActivity";
    EditText mEtName,mEtPwd;
    Button mBtnLogin;

    private void init() {
        mEtName=findViewById(R.id.et_user_name);
        mEtPwd=findViewById(R.id.et_pwd);
        mBtnLogin=findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                final String name = mEtName.getText().toString().trim();
                final String password = mEtPwd.getText().toString().trim();

                if (TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
                    Toast.makeText(this, "请输入完全", Toast.LENGTH_SHORT).show();
                    return;
                }
                OkHttpClient client=new OkHttpClient();
                RequestBody body=new FormBody.Builder()
                        .add("name",name)
                        .add("password",password)
                        .build();
                final Request request=new Request.Builder()
                        .url("http://192.168.1.104:8080/WorldCupApp/user/login")
                        .post(body)
                        .build();

                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.e(TAG, "onFailure: " +e);
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException, IOException {
                        String jsonStr = response.body().string();
                        Log.e(TAG, "onResponse: "+jsonStr );
                        Gson gson=new Gson();
                        User user = gson.fromJson(jsonStr, User.class);
                        if (user.isSuccess()) {
                            SharedPreferences info = getSharedPreferences("User", MODE_PRIVATE);
                            SharedPreferences.Editor edit = info.edit();
                            edit.putString("name", user.getData().getName());
                            edit.putString("password", user.getData().getPassword());
                            edit.putInt("id",user.getData().getId());
                            edit.commit();
                            startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                        }else{
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, "输入的用户名或密码错误", Toast.LENGTH_SHORT).show();
                                }
                            });

                        }
                    }
                });
                break;
        }
    }
}